/**
 *
 */
package com.cloud.advice;

import com.cloud.advice.domain.exception.BusinessException;
import com.cloud.advice.domain.response.ResponseResult;
import com.cloud.advice.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 统一异常处理
 */
@RestControllerAdvice
public class ExceptionHandlerAdviser {

    private static final Logger _logger = LoggerFactory.getLogger(ExceptionHandlerAdviser.class);

    /**
     * 全局处理异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ResponseResult handleException(Exception e) {
        _logger.error("exception ocurred ", e);
        if (e instanceof BusinessException) {
            BusinessException businessException = (BusinessException) e;
            return ResponseUtil.buildExceptionResponse(businessException.getErrorCode(), businessException.getLocalizedMessage());
        }
        return ResponseUtil.buildExceptionResponse(BusinessException.ERROR_CODE, e.getLocalizedMessage());
    }

}
